<template>
  <div class="panel">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="myform"
      label-width="150px"
      status-icon
    >
      <el-form-item label="旧密码" prop="oldPass">
        <el-input
          placeholder="请输入旧密码"
          type="password"
          name="oldPass"
          v-model="ruleForm.oldPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input
          placeholder="请输入新密码"
          type="password"
          name="newPass"
          v-model="ruleForm.newPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          placeholder="再次输入新密码"
          type="password"
          name="checkPass"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click.native.prevent="submitHandle"
          :loading="loading"
          >确定</el-button
        >
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updatePass } from '@/api/group'
export default {
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.myform.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      ruleForm: {
        oldPass: '',
        newPass: '',
        checkPass: ''
      },
      rules: {
        oldPass: [
          { required: true, validator: validateOldPass, trigger: 'blur' }
        ],
        newPass: [
          { required: true, validator: validatePass, trigger: 'blur' },
          { min: 6, message: '密码不能少于6位', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' },
          { min: 6, message: '密码不能少于6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitHandle() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          this.loading = true
          updatePass(this.ruleForm)
            .then((res) => {
              console.log(res)
              this.loading = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
            })
            .catch((res) => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
        }
      })
    },
    resetForm() {
      this.$refs.myform.resetFields()
      this.loading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.panel {
  padding: 50px 24px;
  width: 50%;
}
</style>
